<template>
  <div class="container">
    <div class="center-container">
      <el-form :model="userForm" :rules="rules" ref="userForm" label-width="100px"
               class="demo-userForm">
        <el-form-item label="审核结果" prop="result">
          <el-radio-group v-model="userForm.result" @change="clearValidate">
            <el-radio-button label="1">通过</el-radio-button>
            <el-radio-button label="0">不通过</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="desc"
                      :rules=
                        "[
                          {required: userForm.result!=1, message: '请填写审核意见', trigger: 'blur'},
          {max: 200, message: '审核意见长度不能大于200', trigger: 'blur'}
                        ]">
          <el-input type="textarea" v-model="userForm.desc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('userForm')">保 存</el-button>
          <el-button @click="resetForm('userForm')">重 置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="30%"
      @close="closeDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      center>
      <span><pre>{{ userForm }}</pre></span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "validate-01",
  data() {
    return {
      userForm: {
        result: '1',
        desc: ''
      },
      rules: {
        result: [
          {required: true, message: '请选择审核结果', trigger: 'change'}
        ],
      },
      centerDialogVisible: false,
    };
  },
  methods: {
    closeDialog() {
      this.centerDialogVisible = false;
      this.$refs.userForm.resetFields();
    },
    clearValidate(val) {
      if (val == 1) {
        this.$refs.userForm.clearValidate('desc')
      }
    },
    submitForm(formName) {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          this.centerDialogVisible = true;
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      //this.$refs[formName].resetFields();
      this.$refs.userForm.resetFields();
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* 设置父容器的高度为视口高度，使其充满整个屏幕 */
}

.center-container {
  max-width: 600px; /* 设置内部容器的最大宽度，根据需求进行调整 */
}
</style>
